using System;
using System.Diagnostics;
using System.Globalization;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Microsoft.ApplicationBlocks.Updater
{
	/// <summary>
	/// Helper class for logging and exception reporting.
	/// </summary>
	public sealed class Logger
	{
		#region Declarations

		/// <summary>
		/// The source name for the events.
		/// </summary>
        private const string category = "Microsoft.ApplicationBlocks.Updater"; 

		/// <summary>
		/// The type of the event entry.
		/// </summary>
		//private const EventLogEntryType	defaultLogEntryType	= EventLogEntryType.Information;

		/// <summary>
		/// The default entry Id.
		/// </summary>
		private const int defaultLogEntryId	= 2000;

		/// <summary>
		/// The singleton instance of the helper class.
		/// </summary>
		private static readonly Logger instance = new Logger();

        /// <summary>
        /// The priority of the message.
        /// </summary>
        private static int priority = 1;

		/// <summary>
		/// The EntLib event logger.
		/// </summary>
		//private EventLogger	logger;
		
		#endregion

		#region Constructor

		/// <summary>
		/// Default constructor.
		/// </summary>
		private Logger()
		{
			//logger = new EventLogger( eventLoggerSourceName, defaultLogEntryType, defaultLogEntryId );
		}

		#endregion

		#region Singleton implementation

		/// <summary>
		/// Returns the singleton instance.
		/// </summary>
		private static Logger Current
		{
			get
			{
				return instance;
			}
		}

		#endregion

		#region Public static inteface

		/// <summary>
		/// Log an error message.
		/// </summary>
		/// <param name="message">The message string.</param>
        public static void LogError(string message)
        {
            //Current.WriteLog(message, EventLogEntryType.Error);
            Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(
                message, category, priority, defaultLogEntryId, TraceEventType.Error);
        }
 
        public static void LogError(string message, params object[] args)
        {
            LogError(string.Format(message, args));
        }

		/// <summary>
		/// Log a warning message.
		/// </summary>
		/// <param name="message">The message string.</param>
		public static void LogWarning( string message )
		{
			//Current.WriteLog( message, EventLogEntryType.Warning );
            Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(
                message, category, priority, defaultLogEntryId, TraceEventType.Warning);
        }

        public static void LogWarning(string message, params object[] args)
        {
            LogWarning(string.Format(message, args));
        }

		/// <summary>
		/// Log an information message.
		/// </summary>
		/// <param name="message">The message string.</param>
		public static void LogInformation( string message )
		{
			//Current.WriteLog( message, EventLogEntryType.Information );
            Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(
                message, category, priority, defaultLogEntryId, TraceEventType.Information);
        }

        public static void LogInformation(string message, params object[] args)
        {
            LogInformation(string.Format(message, args));
        }

		/// <summary>
		/// Log an exception.
		/// </summary>
		/// <param name="ex">The exception instance.</param>
		public static void LogException( Exception ex )
		{
			LogException( ex, null );
		}

		/// <summary>
		/// Log an exception and a message.
		/// </summary>
		/// <param name="ex">The exception instance.</param>
		/// <param name="message">The message string.</param>
		public static void LogException( Exception ex, string message )
		{
			ExceptionFormatter formatter = new ExceptionFormatter();
			if ( message != null && message.Length > 0 )
			{
				LogError( message + "\n" + formatter.GetMessage( ex ) );
			}
			else
			{
				LogError( formatter.GetMessage( ex ) );
			}
		}

        public static void LogException(Exception ex, string message, params object[] args)
        {
            LogException(ex, string.Format(message, args));
        }

		/// <summary>
		/// Log and throw the exception specified.
		/// </summary>
		/// <param name="ex">The exception instance.</param>
		public static void LogAndThrowException( Exception ex )
		{
			LogAndThrowException( null, ex );
		}

		/// <summary>
		/// Log and throw the exception specified with a message string.
		/// </summary>
		/// <param name="ex">The exception instance.</param>
		/// <param name="message">The message string.</param>
		public static void LogAndThrowException( string message, Exception ex )
		{
			ExceptionFormatter formatter = new ExceptionFormatter();
			if ( message != null && message.Length > 0 )
			{
				LogError( message + "\n" + formatter.GetMessage( ex ) );
			}
			else
			{
				LogError( formatter.GetMessage( ex ) );
			}
			throw ex;
		}

		#endregion
		
		#region Instance members

		/// <summary>
		/// Write the log to EntLib logger.
		/// </summary>
		/// <param name="message">The message string.</param>
		/// <param name="entryType">The event type.</param>
		private void WriteLog( string message, EventLogEntryType entryType )
		{
			//logger.Log( message, entryType );
		}

		#endregion
	}
}
